//
//  EventItem.m
//  ProcessTimeBudget
//
//  Created by isempty.chiu on 2010/9/13.
//  Copyright 2010 LifeMotion. All rights reserved.
//

#import "EventItem.h"
#import "ProcessMonitor.h"
#import "CommonFunc.h"

@interface RecordItem : NSObject
{
	NSString *ProcessName;
	NSString *ProcessComment;
	
	NSInteger duration_;	//to generate ProcessDuration
	NSDate* Date;	//to generate ProcessStartDate/ProcessStartTime/TimeZoneForDateTime
}

@property (nonatomic, retain) NSString * ProcessName;
@property (nonatomic, readonly) NSString * ProcessDuration;
@property (nonatomic, readonly) NSString * ProcessStartDate;
@property (nonatomic, readonly) NSString * ProcessStartTime;
@property (nonatomic, readonly) NSString * TimeZoneForDateTime;
@property (nonatomic, assign) NSString * ProcessComment;
@property (nonatomic, assign) NSInteger Duration;
@property (nonatomic, retain) NSDate* Date;
@end

@interface ProcessItem : NSObject
{
	NSString *Name;
	NSInteger duration_;
	float Budget;
}

-(ProcessItem*)initWithName:(NSString*)itemName andDuration:(NSInteger)itemDuration andBudget:(float)itemBudget;

@property (nonatomic, readonly) NSString* Name;
@property (nonatomic, assign) NSInteger Duration;
@property (assign) float Budget;

@end

@implementation ProcessItem

@synthesize Name;
@synthesize Duration = duration_;
@synthesize Budget;

-(id) init
{
	self = [super init];	
	if(self)
	{
		Name = nil;
		duration_ = -1;
		Budget = 0;
	}
	
	return self;	
}

-(void) dealloc
{
	if(Name)
		[Name release];
	
	[super dealloc];
}

-(ProcessItem*)initWithName:(NSString*)itemName andDuration:(NSInteger)itemDuration andBudget:(float)itemBudget
{
	Name = [[NSString alloc] initWithString:itemName];
	duration_ = itemDuration;
	Budget = itemBudget;
	
	return self;
}

@end

@implementation RecordItem
-(id) init
{
	// Assign self to value returned by super's designated initializer 
	// Designated initializer for NSObject is init 
	
	self = [super init];	
	if(self)
	{
		ProcessName = nil;
		ProcessComment = nil;
		Date = nil;
		
		duration_ = 0;
	}
	
	return self;	
}

- (void) dealloc
{
	if(ProcessName != nil)
		[ProcessName release];
	if(ProcessComment != nil)
		[ProcessComment release];
	if(Date != nil)
		[Date release];
	
	[super dealloc];
}

@synthesize ProcessName;
@synthesize Date;
@synthesize Duration = duration_;

-(NSString*) ProcessComment
{
	return ProcessComment == nil ? @"None" : ProcessComment;
}

-(void) setProcessComment:(NSString *)comment
{
	ProcessComment = [[NSString alloc] initWithString:comment];
}

-(NSString*) ProcessDuration
{
	return [NSString stringWithFormat:@"%@", [CommonFunc getStringFromTimeInterval:duration_]];
}

-(NSString*) ProcessStartDate
{
	return [NSString stringWithFormat:@"%@", [CommonFunc getDateString:Date byFormat:@"yyyy-MM-dd"]];
}

-(NSString*) ProcessStartTime
{
	return [NSString stringWithFormat:@"%@", [CommonFunc getDateString:Date byFormat:@"HH:mm:ss"]];
}

-(NSString*) TimeZoneForDateTime
{
	return [NSString stringWithFormat:@"%@", [CommonFunc getDateString:Date byFormat:@"ZZZ"]];
}

@end

@implementation EventItem

-(id) init
{
	self = [super init];	
	if(self)
	{
		processRecords_ = [[NSMutableArray alloc] init]; 
		eventBudget_ = 0;
		eventDuration_ = [ProcessMonitor isRunningUnitTest] ? 59983 : 0;
		processList_ = [[NSMutableArray alloc] init];
	}
	
	return self;
}

-(void) dealloc
{
	[processList_ removeAllObjects];
	[processRecords_ removeAllObjects];
	[processRecords_ release];
	[processList_ release];
	
	[super dealloc];
}

@synthesize RecordItemList = processRecords_;
@synthesize ProcessItemList = processList_;
@synthesize Budget = eventBudget_;
@synthesize Name = eventName_;
-(NSInteger)Duration
{
	return eventDuration_ / 2;
}

- (ProcessItem*)getProcessItemByName:(NSString*)name
{
	for(ProcessItem* item in processList_)
	{
		if([item.Name isEqualToString:name])
			return item;
	}
	
	return nil;
}

- (BOOL) updateIfEqual:(NSString*)candidateAppName
{
	if([processRecords_ count] > 0)
	{
		RecordItem*	item = (RecordItem*)[processRecords_ objectAtIndex:0];
		if([item.ProcessName isEqualToString:candidateAppName])
		{
			item.Duration = item.Duration + (NSInteger)[[ProcessMonitor sharedProcMonitor] ActiveDuration];
			ProcessItem* pItem = [self getProcessItemByName:item.ProcessName];
			pItem.Duration = pItem.Duration + (NSInteger)[[ProcessMonitor sharedProcMonitor] ActiveDuration];
			return YES;
		}
	}
	
	return NO;
}

-(void)timerNotify
{
	eventDuration_ = eventDuration_ + 1;
}

- (void) reset
{
	[processRecords_ removeAllObjects];
	[processList_ removeAllObjects];
	eventDuration_ = 0;
}

-(void)activeChangedNotify
{
	if(![self updateIfEqual: [[ProcessMonitor sharedProcMonitor] ActiveProcessName]])
	{
		ProcessItem* pItem = [self getProcessItemByName:[[ProcessMonitor sharedProcMonitor] ActiveProcessName]];
		if(pItem == nil)
		{
			//not found, create a new one
			pItem = [[ProcessItem alloc] initWithName:[[ProcessMonitor sharedProcMonitor] ActiveProcessName] andDuration:[[ProcessMonitor sharedProcMonitor] ActiveDuration] andBudget:0];
			[processList_ insertObject:pItem atIndex:0];
			[pItem release];
		}
		else {
			pItem.Duration = pItem.Duration + (NSInteger)[[ProcessMonitor sharedProcMonitor] ActiveDuration];
		}

		RecordItem*	item = [[RecordItem alloc]init];
		item.ProcessName = pItem.Name;
		item.Duration = (NSInteger)[[ProcessMonitor sharedProcMonitor] ActiveDuration];
		item.Date = [[ProcessMonitor sharedProcMonitor] ActiveStartTime];
				
		[processRecords_ insertObject:item atIndex:0];		
		[item release];
	}	
}

@end
